package com.sip.stream.utils.zfy.handler;

import android.content.Context;
import android.os.Build;
import android.os.Looper;
import android.util.Base64;
import android.util.Log;
import androidx.exifinterface.media.ExifInterface;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.StringUtils;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.sip.stream.ClientApp;
import com.sip.stream.R;
import com.sip.stream.gbstack.GS28181Result;
import com.sip.stream.gbstack.GS28181SDK;
import com.sip.stream.gbstack.GS28181SDKCallBack;
import com.sip.stream.gbstack.GS28181SDKConfig;
import com.sip.stream.helper.DocumentHelper;
import com.sip.stream.helper.GS28181Helper;
import com.sip.stream.rxbus.RxBus;
import com.sip.stream.utils.zfy.PwitheTranCmd;
import com.sip.stream.utils.zfy.RxBusTag;
import com.sip.stream.utils.zfy.voice.BeepType;
import com.sip.stream.utils.zfy.GSCTranCmd;
import com.sip.stream.utils.zfy.ZFYFilePath;
import com.sip.stream.utils.zfy.ZFYLocation;
import com.sip.stream.utils.zfy.ZFYPlatformRemoteText;
import com.sip.stream.utils.zfy.ZFYUserInfo;
import com.sip.stream.utils.zfy.event.ZFYMediaEvent;
import com.sip.stream.utils.zfy.status.ZFYStatus;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.Single;
import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.BehaviorSubject;

import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class GSCGB28181TranHandler extends IGB28181TranHandler {
    public static final String TAG = "GSCGB28181TranHandler";
    public static boolean isFirstRegisterSuccess = true;
    public Scheduler backgroundScheduler;
    public BehaviorSubject<Boolean> bindUserBehaviorSubject;
    private String cachePassword;
    private String cacheUserID;
    private DocumentHelper documentHelper;
    private Gson gson;
    private final Format locationDateFormate;
    private Disposable postDeviceInfoDisposable;
    private Disposable postLocationInfoDisposable;

    private void emergencyInfo(Document document) {
    }

    public GSCGB28181TranHandler() {
        this.locationDateFormate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        this.bindUserBehaviorSubject = null;
        this.backgroundScheduler = Schedulers.single();
        this.documentHelper = new DocumentHelper();
    }

    public GSCGB28181TranHandler(Looper looper) {
        this.locationDateFormate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        this.bindUserBehaviorSubject = null;
        this.backgroundScheduler = AndroidSchedulers.from(looper);
        this.documentHelper = new DocumentHelper();
    }

    @Override
    public void afterRegisterSuccess() {
        Log.i(TAG, "afterRegisterSuccess");
        postDeviceInfo();
        postLocationInfo();
        Log.i(TAG, "postLocationInfo");
        if (isFirstRegisterSuccess) {
            isFirstRegisterSuccess = false;
            sendActionLog("01");
        }
        Log.i(TAG, "sendActionLog");

    }


    private Context getContext() {
        return ClientApp.clientApp;
    }

    @Override
    public void postDeviceSOS() {
        HashMap hashMap = new HashMap();
        hashMap.put("Datetime", this.locationDateFormate.format(new Date()));
        HashMap sosMap = new HashMap();
        if (putLocationInfo(sosMap))
        {
            hashMap.put("Location", sosMap);
            sendTransData(createTranData("DeviceSOS", "DeviceSOSInfo", hashMap));
        }
    }

    @Override
    public int onTransDataNotify(String str, int i) {
        Single.just(str).observeOn(this.backgroundScheduler).subscribe(new SingleObserver<String>() {
            @Override
            public void onSubscribe(Disposable disposable) {
            }

            @Override
            public void onSuccess(String str2) {
                GSCGB28181TranHandler.this.onTransTDataNotify(str2);
            }

            @Override
            public void onError(Throwable th) {
                th.printStackTrace();
            }
        });
        return 200;
    }

    @Override
    public void postAlarmTypeToPlatform(String type, String message) {

    }

    @Override
    public Single<Boolean> bindUser(String userID, String password) {
        if (StringUtils.isEmpty(userID) || StringUtils.isEmpty(password)) {
            return Single.error(new Throwable("cacheUserID or pCachePassword null"));
        }
        this.bindUserBehaviorSubject = BehaviorSubject.create();
        Log.i(TAG, "bindUser " + userID + " " + password);
        HashMap hashMap = new HashMap();
        hashMap.put("PlatformID", "GM_SHENZHEN");
        hashMap.put("DeviceIMEI", "IMEI");
        hashMap.put("DeviceSN", "DeviceSN");
        hashMap.put("UserID", userID);
        hashMap.put("Password", password);
        this.cacheUserID = userID;
        this.cachePassword = password;
        sendTransData(createTranData("BindUser", "DeviceStates", hashMap));
        return this.bindUserBehaviorSubject.firstOrError().timeout(30L, TimeUnit.SECONDS, Single.error(new Throwable(GS28181Helper.getInstance().getContext().getString(R.string.binduser_timeout)))).doFinally(new Action() {
            @Override
            public void run() {
                bindUserBehaviorSubject = null;
            }
        });
    }

    public void onTransTDataNotify(String str) {
        String str2 = "<GXXROOT>" + str + "</GXXROOT>";
        Log.i(TAG, "reciever onTransDataNotify " + str2);
        try {
            Document parse = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(str2)));
            NodeList childNodes = parse.getFirstChild().getChildNodes();
            if (childNodes == null || childNodes.getLength() == 0) {
                Log.w(TAG, "tNodeList = null || tNodeList.getLength() == 0");
            }
            for (int i = 0; i < childNodes.getLength(); i++) {
                Log.w(TAG, "for i " + i);
                Node item = childNodes.item(i);
                if (item == null) {
                    Log.w(TAG, "Node  null");
                } else if ("SubCmdType".equalsIgnoreCase(item.getNodeName())) {
                    handlerSubCmd(item.getFirstChild().getNodeValue(), parse);
                    return;
                }
            }
        } catch (IOException | ParserConfigurationException | SAXException e) {
            Log.e(TAG, "" + e.getLocalizedMessage());
            e.printStackTrace();
        }
    }

    private void handlerSubCmd(String str, Document document) {
        if (str == null) {
            return;
        }
        Log.i(TAG, "handlerSubCmd: " + str);
        str.hashCode();
        char c = 65535;
        switch (str.hashCode()) {
            case -1777650656:
                if (str.equals(GSCTranCmd.CMD_ImportantPersonNotify)) {
                    c = 0;
                    break;
                }
                break;
            case -1775974884:
                if (str.equals(GSCTranCmd.CMD_BizNotify)) {
                    c = 1;
                    break;
                }
                break;
            case -1265861281:
                if (str.equals(GSCTranCmd.CMD_UnbindNotify)) {
                    c = 2;
                    break;
                }
                break;
            case -1263886290:
                if (str.equals(GSCTranCmd.CMD_RemoteBindUser)) {
                    c = 3;
                    break;
                }
                break;
            case -1250637183:
                if (str.equals("RemoteUpdateSetting")) {
                    c = 4;
                    break;
                }
                break;
            case -1207970161:
                if (str.equals(GSCTranCmd.CMD_RemoteTextDisplay)) {
                    c = 5;
                    break;
                }
                break;
            case -978322405:
                if (str.equals(GSCTranCmd.CMD_MobileDeviceManagement)) {
                    c = 6;
                    break;
                }
                break;
            case -857867823:
                if (str.equals(GSCTranCmd.CMD_RemoteClearData)) {
                    c = 7;
                    break;
                }
                break;
            case -701963797:
                if (str.equals("RemoteFactoryReset")) {
                    c = '\b';
                    break;
                }
                break;
            case -698946600:
                if (str.equals(GSCTranCmd.CMD_BindUserConfirm)) {
                    c = '\t';
                    break;
                }
                break;
            case -428370160:
                if (str.equals("RemoteAudio")) {
                    c = '\n';
                    break;
                }
                break;
            case -143420273:
                if (str.equals(GSCTranCmd.CMD_DeviceLogUpload)) {
                    c = 11;
                    break;
                }
                break;
            case 77292459:
                if (str.equals("RemoteReboot")) {
                    c = '\f';
                    break;
                }
                break;
            case 77322327:
                if (str.equals(PwitheTranCmd.CMD_RemoteRecord)) {
                    c = '\r';
                    break;
                }
                break;
            case 619421119:
                if (str.equals("EmergencyInfo")) {
                    c = 14;
                    break;
                }
                break;
            case 715142567:
                if (str.equals(PwitheTranCmd.CMD_RemoteDeviceLock)) {
                    c = 15;
                    break;
                }
                break;
            case 950062307:
                if (str.equals(GSCTranCmd.CMD_FaceResultNotify)) {
                    c = 16;
                    break;
                }
                break;
            case 1004175816:
                if (str.equals("BindUser")) {
                    c = 17;
                    break;
                }
                break;
            case 1139430923:
                if (str.equals("P2DUpdate")) {
                    c = 18;
                    break;
                }
                break;
            case 1898357797:
                if (str.equals("RemoteTakePhoto")) {
                    c = 19;
                    break;
                }
                break;
            case 1921252604:
                if (str.equals("RemoteShutDown")) {
                    c = 20;
                    break;
                }
                break;
        }
        switch (c) {
            case 0:
                //重点人通知
                importantPersonNotify(document);
                return;
            case 1:
                BizNotify(document);
                return;
            case 2:
                unbindNotify(document);
                return;
            case 3:
                remoteBindUser(document);
                return;
            case 4:
                remoteUpdateSetting(document);
                return;
            case 5:
                RemoteTextDisplay(document);
                return;
            case 6:
                MobileDeviceManagement(document);
                return;
            case 7:
                //远程-清空数据
                //ZFYCore.getRounter().clearData();
                return;
            case '\b':
                postSubCmdResult("RemoteFactoryReset", "SUCCEES");
                //ZFYCore.getRounter().factoryReset();
                return;
            case '\t':
                //远程-绑定自定义用户
                postSubCmdResult(PwitheTranCmd.CMD_BindUserConfirm, "");
                return;
            case '\n':
                //远程-音频通话
                String stringValue = this.documentHelper.getStringValue(document, "SubCmdOperator");
                if (stringValue == null) {
                    postSubCmdResult("RemoteAudio", "FAILED");
                    return;
                }
                if ("start".equalsIgnoreCase(stringValue)) {
                    RxBus.getDefault().post(new ZFYMediaEvent(ZFYMediaEvent.EVENT_TYPE_REMOTE_AUDIO_START), RxBusTag.MEDIA_EVENT);
                } else {
                    RxBus.getDefault().post(new ZFYMediaEvent(ZFYMediaEvent.EVENT_TYPE_REMOTE_AUDIO_STOP), RxBusTag.MEDIA_EVENT);
                }
                postSubCmdResult("RemoteAudio", "SUCCEES");
                return;
            case 11:
                deviceLogUpload(document);
                return;
            case '\f':
                //远程-重启
                postSubCmdResult("RemoteReboot", "SUCCEES");
                //ZFYCore.getRounter().reBoot();
                return;
            case '\r':
                //远程-录制
                String stringValue2 = this.documentHelper.getStringValue(document, "SubCmdOperator");
                if (stringValue2 == null) {
                    postSubCmdResult(PwitheTranCmd.CMD_RemoteRecord, "FAILED");
                    return;
                }
                if ("start".equalsIgnoreCase(stringValue2)) {
                    RxBus.getDefault().post(new ZFYMediaEvent(ZFYMediaEvent.EVENT_TYPE_REMOTE_VIDEO_START), RxBusTag.MEDIA_EVENT);
                } else {
                    RxBus.getDefault().post(new ZFYMediaEvent(ZFYMediaEvent.EVENT_TYPE_REMOTE_VIDEO_STOP), RxBusTag.MEDIA_EVENT);
                }
                postSubCmdResult(PwitheTranCmd.CMD_RemoteRecord, "SUCCEES");
                return;
            case 14:
                emergencyInfo(document);
                postSubCmdResult("EmergencyInfo", "SUCCEES");
                return;
            case 15:
                //远程-锁定设备
                //ZFYCore.getRounter().lockDevice(!"Unlock".equalsIgnoreCase(this.documentHelper.getStringValue(document, "SubCmdOperator")));
                postSubCmdResult(PwitheTranCmd.CMD_RemoteDeviceLock, "SUCCEES");
                return;
            case 16:
                //人脸绑定结果
                //FaceResultNotify(document);
                return;
            case 17:
                bindUserResult(document);
                return;
            case 18:
                p2DUpdate(document);
                return;
            case 19:
                RxBus.getDefault().post(new ZFYMediaEvent(ZFYMediaEvent.EVENT_TYPE_REMOTE_CAPUTRE), RxBusTag.MEDIA_EVENT);
                postSubCmdResult("RemoteTakePhoto", "SUCCEES");
                return;
            case 20:
                //远程-关机
                postSubCmdResult("RemoteShutDown", "SUCCEES");
                //ZFYCore.getRounter().shutDown();
                return;
            default:
                return;
        }
    }

    private void remoteUpdateSetting(Document document) {
        try {
            UpdateSetting(document);
            postSubCmdResult("RemoteUpdateSetting", "SUCCEES");
        } catch (Exception e) {
            postSubCmdResult("RemoteUpdateSetting", e.getMessage());
        }
    }

    private void UpdateSetting(Document document) throws Exception {
        JsonObject jsonObject = new JsonObject();
        JsonObject jsonObject2 = new JsonObject();
        if (document == null) {
            Log.e(TAG, "document == null");
            return;
        }
        Node childNode = this.documentHelper.getChildNode(document, "UpdateSetting");
        if (childNode == null) {
            return;
        }
        for (int i = 0; i < childNode.getChildNodes().getLength(); i++) {
            Node item = childNode.getChildNodes().item(i);
            for (int i2 = 0; i2 < item.getChildNodes().getLength(); i2++) {
                Node item2 = item.getChildNodes().item(i2);
                if (item2 != null) {
                    String nodeName = item2.getNodeName();
                    String nodeValue = item2.getFirstChild() != null ? item2.getFirstChild().getNodeValue() : null;
                    nodeName.hashCode();
                    char c = 65535;
                    switch (nodeName.hashCode()) {
                        case -1752163770:
                            if (nodeName.equals("UserID")) {
                                c = 0;
                                break;
                            }
                            break;
                        case -1076860134:
                            if (nodeName.equals("DepName")) {
                                c = 1;
                                break;
                            }
                            break;
                        case -201069322:
                            if (nodeName.equals("Username")) {
                                c = 2;
                                break;
                            }
                            break;
                        case 65918282:
                            if (nodeName.equals("DepID")) {
                                c = 3;
                                break;
                            }
                            break;
                        case 105960021:
                            if (nodeName.equals("LocationRate")) {
                                c = 4;
                                break;
                            }
                            break;
                        case 1471348843:
                            if (nodeName.equals("PositionStopRate")) {
                                c = 5;
                                break;
                            }
                            break;
                        case 1887537432:
                            if (nodeName.equals("DeviceStatesRate")) {
                                c = 6;
                                break;
                            }
                            break;
                    }
                    switch (c) {
                        case 0:
                            jsonObject.addProperty("userCode", nodeValue);
                            break;
                        case 1:
                            jsonObject.addProperty("unitName", nodeValue);
                            break;
                        case 2:
                            jsonObject.addProperty("userName", nodeValue);
                            break;
                        case 3:
                            jsonObject.addProperty("unitCode", nodeValue);
                            break;
                        case 4:
                            new GS28181SDKConfig.Builder().setLocationRate(Integer.parseInt(nodeValue)).commit(null);
                            break;
                        case 5:
                            new GS28181SDKConfig.Builder().setPositionStopRate(Integer.parseInt(nodeValue)).commit(null);
                            break;
                        case 6:
                            int parseInt = Integer.parseInt(nodeValue);
                            Log.i(TAG, "DeviceStatesRate " + parseInt);
                            new GS28181SDKConfig.Builder().setDeviceStatesRate(parseInt).commit(null);
                            break;
                        default:
                            Log.i(TAG, "addProperty " + nodeName + " " + nodeValue);
                            jsonObject2.addProperty(nodeName, nodeValue);
                            break;
                    }
                }
            }
        }
        if (jsonObject.size() > 0) {
            JsonObject asJsonObject = getGson().toJsonTree(ZFYUserInfo.defaultUserInfo()).getAsJsonObject();
            updateJson(asJsonObject, jsonObject);
            ZFYUserInfo zFYUserInfo = (ZFYUserInfo) getGson().fromJson((JsonElement) asJsonObject,  ZFYUserInfo.class);
            Log.i(TAG, "changeUserInfo " + getGson().toJson(zFYUserInfo));
            /**
             * 切换本地用户信息操作
             */
        }
        Log.i(TAG, "saveSettings");
        Log.i(TAG, "saveMediaConfig");
        //保存所有配置到本地存储刷新当前实时的配置
    }

    private void updateJson(JsonObject jsonObject, JsonObject jsonObject2) {
        for (Map.Entry<String, JsonElement> entry : jsonObject2.entrySet()) {
            jsonObject.add(entry.getKey(), entry.getValue());
        }
    }

    private Gson getGson() {
        if (this.gson == null) {
            this.gson = new Gson();
        }
        return this.gson;
    }

    public void postSubCmdResult(String action, String message) {
        String xmlBody = "<SubCmdType>" + action + "</SubCmdType>";
        if (message != null) {
            xmlBody = xmlBody + "<SubCmdResult>" + message + "</SubCmdResult>";
        }
        sendTransData(xmlBody);
    }

    //
    private void MobileDeviceManagement(Document document) {
    }


    //核心通知
    private void BizNotify(Document document) {
        try {
            postSubCmdResult(GSCTranCmd.CMD_BizNotify, "SUCCEES");
        } catch (Exception e) {
            e.printStackTrace();
            postSubCmdResult(GSCTranCmd.CMD_BizNotify, "FAILED");
        }
    }

    //设备日志上报通知
    private void deviceLogUpload(Document document) {
        Log.i(TAG, "deviceLogUpload");
        postSubCmdResult(GSCTranCmd.CMD_DeviceLogUpload, "SUCCEES");
    }

    //发送事件日志
    private void sendActionLog(String action) {
        sendTransData("<SubCmdType>DSJActionLog</SubCmdType><ActionType>" + action + "</ActionType>");
    }


    //远程绑定用户信息
    private void remoteBindUser(Document document) {

    }



    //绑定用户结果反馈
    private void bindUserResult(Document document) {

    }


    //人员导入通知
    private void importantPersonNotify(Document document) {
        postSubCmdResult(GSCTranCmd.CMD_ImportantPersonNotify, "SUCCEES");
    }

    //私标更新
    private void p2DUpdate(Document document) {
        postSubCmdResult("P2DUpdate", "SUCCEES");
    }


    //解绑通知
    private void unbindNotify(Document document) {
        Log.w(TAG, "28181 unbindNotify");
        postSubCmdResult(GSCTranCmd.CMD_UnbindNotify, "RECEIVED");
    }

    private String createTranData(String str, String str2, Object obj) {
        final StringBuilder sb = new StringBuilder();
        sb.append("<SubCmdType>");
        sb.append(str);
        sb.append("</SubCmdType>");
        sb.append("<" + str2 + ">");
        if (obj instanceof Map) {
            final Gson gson = new Gson();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                ((Map) gson.fromJson(gson.toJson(obj), Map.class)).forEach(new BiConsumer() {
                    @Override
                    public  void accept(Object obj2, Object obj3) {
                        GSCGB28181TranHandler.lambda$createTranData$3(sb, gson, obj2, obj3);
                    }
                });
            }
        } else {
            sb.append("" + obj);
        }
        sb.append("</" + str2 + ">");
        return sb.toString();
    }

    public static  void lambda$createTranData$3(final StringBuilder sb, Gson gson, Object obj, Object obj2) {
        sb.append("<" + obj + ">");
        if (obj2 instanceof Map) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                ((Map) gson.fromJson(gson.toJson(obj2), Map.class)).forEach(new BiConsumer() {
                    @Override
                    public  void accept(Object obj3, Object obj4) {
                        sb.append("<" + obj3 + ">" + obj4 + "</" + obj3 + ">");
                    }
                });
            }
        } else {
            sb.append(obj2);
        }
        sb.append("</" + obj + ">");
    }

    //发送设备信息
    private void postDeviceInfo() {
        if (this.postDeviceInfoDisposable != null) {
            this.postDeviceInfoDisposable.dispose();
            this.postDeviceInfoDisposable = null;
        }
        Observable.just(Boolean.TRUE).map(new Function() {
            @Override
            public  Object apply(Object obj) {
                HashMap hashMap = new HashMap();
                hashMap.put("Carrieroperator", "0");
                hashMap.put("Storage", "" + ZFYFilePath.getInstance().getEmptyGB());
                hashMap.put("Version", GS28181SDK.getInstance().getConfig().getFirmware());
                hashMap.put("ConfigVersion", GS28181SDK.getInstance().getConfig().getConfigVersion());
                hashMap.put("LocalRecord", "NO");
                hashMap.put("ChargeState", "NO");
                hashMap.put("Nettype", "" + ZFYStatus.getInstance().getMobileNetworkType());
                hashMap.put("Signal", "" + (ZFYStatus.getInstance().getMobileSignalStrength() * 20));
                hashMap.put("Battery", "" + ZFYStatus.getInstance().getBatteryLevel());
                hashMap.put("CPUModelNo", "4C");
                sendTransData(createTranData("DeviceInfo", "DeviceStates", hashMap));
                return Boolean.TRUE;
            }
        }).repeatWhen(new Function() {
            @Override
            public  Object apply(Object obj) {
                return ((Observable) obj).delay(getConfig().getDeviceStatesRate(), TimeUnit.MILLISECONDS);
            }
        }).subscribe(new Observer<Boolean>() {
            @Override
            public void onComplete() {
            }

            @Override
            public void onNext(Boolean bool) {
            }

            @Override
            public void onSubscribe(Disposable disposable2) {
                GSCGB28181TranHandler.this.postDeviceInfoDisposable = disposable2;
                GSCGB28181TranHandler.this.addDisposable(disposable2);
            }

            @Override
            public void onError(Throwable th) {
                th.printStackTrace();
            }
        });
    }


    //发送位置信息
    private void postLocationInfo() {
        if (this.postLocationInfoDisposable != null) {
            this.postLocationInfoDisposable.dispose();
            this.postLocationInfoDisposable = null;
        }
        Observable.just(Boolean.TRUE).delay(getConfig().getLocationRate(), TimeUnit.MILLISECONDS).map(new Function() {
            @Override
            public  Object apply(Object obj) {
                Map<String, String> gpsMap = new HashMap<>();
                putLocationInfo(gpsMap);
                sendTransData(createTranData("LocationInfo", "Location", gpsMap));
                return Boolean.TRUE;
            }
        }).repeat().subscribe(new Observer<Boolean>() {
            @Override
            public void onComplete() {
            }

            @Override
            public void onNext(Boolean bool) {
            }

            @Override
            public void onSubscribe(Disposable disposable2) {
                GSCGB28181TranHandler.this.postLocationInfoDisposable = disposable2;
                GSCGB28181TranHandler.this.addDisposable(disposable2);
            }

            @Override
            public void onError(Throwable th) {
                th.printStackTrace();
            }
        });
    }


    /***
     * 当前位置信息组装
     * @param map
     * @return
     */
    private boolean putLocationInfo(Map<String, String> map) {
        ZFYLocation cacheLocation = GS28181Helper.getInstance().getCacheLocation();
        double latitude = ZFYStatus.getInstance().getLatitude();
        double longitude = ZFYStatus.getInstance().getLongitude();
        String str = latitude >= 0.0d ? "N" : ExifInterface.LATITUDE_SOUTH;
        String str2 = longitude >= 0.0d ? ExifInterface.LONGITUDE_EAST : ExifInterface.LONGITUDE_WEST;
        map.put("DivisionNS", str);
        map.put("DivisionEW", str2);
        map.put("Radius", "3");
        map.put("Longitude", String.format("%.8f", Double.valueOf(longitude)));
        map.put("Latitude", String.format("%.8f", Double.valueOf(latitude)));
        map.put("Direction", String.format("%.2f", Float.valueOf(cacheLocation.getBearingAccuracyDegrees())));
        map.put("Speed", "" + cacheLocation.getSpeed());
        map.put("Height", "" + cacheLocation.getAltitude());
        map.put("LocationTime", this.locationDateFormate.format(new Date(cacheLocation.getLocationTime())));
        map.put("Satellites", "" + cacheLocation.getSatellites());
        map.put("LocationAvailable", "1");
        return true;
    }



    public void configUpdateReceipt(boolean z, String str) {
        HashMap hashMap = new HashMap();
        hashMap.put("UpdateResult", z ? "SUCCESS" : "FAILED");
        hashMap.put("UpdateResultDetail", str);
        sendTransData(createTranData("ConfigUpdateReceipt", "ConfigUpdateReport", hashMap));
    }


    /**
     * 发送远程文本信息
     * @param zFYPlatformRemoteText
     * @return
     */
    @Override
    public Single<Boolean> postRemoteText(ZFYPlatformRemoteText zFYPlatformRemoteText)
    {
        return Single.just(zFYPlatformRemoteText).map(new Function() {
            @Override
            public Object apply(Object obj) {
                String data;
                StringBuilder sb = new StringBuilder();
                sb.append("<SubCmdType>");
                sb.append(GSCTranCmd.CMD_DeviceTextDisplay);
                sb.append("</SubCmdType>");
                sb.append("<Data>");
                try {
                    data = Base64.encodeToString(zFYPlatformRemoteText.getData().getBytes(StandardCharsets.UTF_8), 2);
                } catch (Exception unused) {
                    data = zFYPlatformRemoteText.getData();
                }
                sb.append(data);
                sb.append("</Data>");
                if (zFYPlatformRemoteText.getReplyTo() != null) {
                    sb.append(zFYPlatformRemoteText.getReplyTo());
                }
                String sb2 = sb.toString();
                Log.i(TAG, "postRemoteText " + sb2);
                GS28181SDK.getInstance().postTransDatas(sb2, new GS28181SDKCallBack() {
                    @Override
                    public  void onComplete(GS28181Result gS28181Result) {
                        if (gS28181Result.isSuccess()) {
                            Log.i(TAG, "postRemoteText success");
                        } else {
                            Log.w(TAG, "postRemoteText error" + gS28181Result.message);
                        }
                    }
                });
                return Boolean.TRUE;
            }
        });
    }


    /**
     * 远程文本显示
     * @param document
     */
    private void RemoteTextDisplay(Document document) {
        try {
            ZFYPlatformRemoteText zFYPlatformRemoteText = new ZFYPlatformRemoteText();
            zFYPlatformRemoteText.setData(this.documentHelper.getStringValue(document, "Data"));
            zFYPlatformRemoteText.setShowName(this.documentHelper.getStringValue(document, "ShowName"));
            String nodeToString = this.documentHelper.nodeToString(this.documentHelper.getChildNode(document, "ReplyTo"));
            Log.i(TAG, "ReplyToStr " + nodeToString);
            zFYPlatformRemoteText.setReplyTo(nodeToString);
            String stringValue = this.documentHelper.getStringValue(document, "PromptTone");
            if (stringValue != null) {
                int i = 0;
                try {
                    i = Integer.parseInt(stringValue);
                } catch (Exception unused) {
                }
                zFYPlatformRemoteText.setPromptTone(i);
            }
            Log.i(TAG,"远程文本预览： " + GsonUtils.toJson(zFYPlatformRemoteText));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //远程人脸绑定
    @Override
    public Single<Boolean> startFaceBind(String str) {
        return Single.just(str).observeOn(Schedulers.io()).flatMap(new Function() {
            @Override
            public  Object apply(Object obj) {
                return true;
            }
        }).flatMap(new Function() {
            @Override
            public  Object apply(Object obj) {
                return true;
            }
        });
    }



    @Override
    public void reportAction(String str, String str2) {
        super.reportAction(str, str2);
        Log.i(TAG, "reportAction " + str + " " + str2);
        if (!"dsjAction".equalsIgnoreCase(str) || StringUtils.isEmpty(str2)) {
            return;
        }
        str2.hashCode();
        char c = 65535;
        switch (str2.hashCode()) {
            case -1524497517:
                if (str2.equals("MOBILE_SETTING")) {
                    c = 0;
                    break;
                }
                break;
            case -1483131800:
                if (str2.equals(BeepType.DEVICE_ON)) {
                    c = 1;
                    break;
                }
                break;
            case -1235519035:
                if (str2.equals("VPN_SETTING")) {
                    c = 2;
                    break;
                }
                break;
            case -637307292:
                if (str2.equals("PLATFORM_SETTING")) {
                    c = 3;
                    break;
                }
                break;
            case 387077098:
                if (str2.equals("BAT_LOW")) {
                    c = 4;
                    break;
                }
                break;
            case 387080164:
                if (str2.equals("BAT_OUT")) {
                    c = 5;
                    break;
                }
                break;
            case 1095239090:
                if (str2.equals("SIGN_LOW")) {
                    c = 6;
                    break;
                }
                break;
            case 1267554278:
                if (str2.equals(BeepType.DEVICE_OFF)) {
                    c = 7;
                    break;
                }
                break;
            case 1952148911:
                if (str2.equals("BAT_IN")) {
                    c = '\b';
                    break;
                }
                break;
            case 2140721136:
                if (str2.equals("APN_SETTING")) {
                    c = '\t';
                    break;
                }
                break;
        }
        switch (c) {
            case 0:
                sendActionLog("13");
                return;
            case 1:
                sendActionLog("01");
                return;
            case 2:
                sendActionLog("15");
                return;
            case 3:
                sendActionLog("16");
                return;
            case 4:
                sendActionLog("19");
                return;
            case 5:
                sendActionLog("11");
                return;
            case 6:
                sendActionLog("18");
                return;
            case 7:
                sendActionLog("02");
                return;
            case '\b':
                sendActionLog("12");
                return;
            case '\t':
                sendActionLog("14");
                return;
            default:
                sendActionLog("99");
                return;
        }
    }
}
